<?php
namespace Bshop\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
    	$this->display('Login/login');
    }
    public function login(){
    	//session_destroy();
    	$this->display('Login/login');
    }
    public function cklogin(){
    	$username = I('post.shop_username');
    	$password = I('post.shop_password');
    	$verifyy = I('post.shop_verify');
    	$verify = new \Think\Verify();
    	$a = $verify->check($verifyy);
    	if($a){
    		$db = D('Common/Shop');
    		$data['username'] = $username;
    		$data['password'] = $db->autopassword($password);
    		$data['close'] = 0;
    		//print_R($data);exit();
    		$re = $db->where($data)->find();
    		if(!empty($re)){
    			session('bxd_shop_user_id',$re['id']);
    			/* $map['login_ip'] = get_client_ip();
    			$map['login_time'] = date("Y-m-d H:i:s");
    			$map['id'] = $re['id'];
    			$db->save($map); */
    			$this->redirect('/'.MODULE_NAME."/");
    		}else{
    			$this->login();
    			echo showmsg('帐号或密码输入错误');exit;
    		}
    	}else{
    		$this->login();
    		echo showmsg('验证码输入错误');
    	}
    }
    public function register(){
    	$res = $_POST;
    	$this->assign('va',$res);
    	$this->display('Login/register');
    }
    /**
     * 注册商家
     */
    public function save_shop(){
    	$db = D('Common/Shop');
    	if($create = $db->create()){
    		$code = I('post.code');
    		$newpass = $create['password'];
    		$ckpass = I('post.ckpassword'); 
    		if($newpass!=$ckpass){
    			$this->register();
    			echo showmsg('两次密码输入不正确');exit;
    		}else{
    			$online = I('post.online');
    			if($online!=1){
    				$this->register();
    				echo showmsg('请同意商户服务协议');exit;
    			}
    			$verifyy = I('post.shop_verify');
    			$verify = new \Think\Verify();
    			$aa = $verify->check($verifyy);
    			if($aa){
    				$a = \Org\Oa\msg::check_phone_code($create['username'],$code);
    				if($a){
    					$create['password'] = $db->autopassword($newpass);
    					$create['phone'] = $create['username'];
    					$create['close'] = 1;
    					$re = $db->add($create);
    					if($re!=false){
    						M('phone_code')->where(array('phone'=>$create['username']))->setField('is_use',1);
    						//$_SESSION['eventor_shop_user_id'] = $re;
    						session('bxd_shop_user_id',$re);
    						$this->register();
    						echo alert('注册成功,现在去完善资料吧',C('URL')."/Index/index");exit;
    					}else{
    						$this->register();
    						echo showmsg('注册失败，请重新注册');exit;
    					}
    				}else{
    					$this->register();
    					echo showmsg('手机验证码输入错误');exit;
    				}
    			}else{
    				$this->register();
    				echo showmsg('验证码输入错误');exit;
    			}
    			
    		}
    	}else{
    		$this->register();
    		echo showmsg($db->getError());exit;
    	}
    }
    /**
     * 找回密码
     */
    public function update_password(){
    	$username = I('post.username');
    	$code = I('post.code');
    	$password = I('post.password');
    	$ckpassword = I('post.ckpassword');
    	$shop_verify = I('post.shop_verify');
    	$verify = new \Think\Verify();
    	$aa = $verify->check($shop_verify);
    	if($aa){
    		$db = D('Common/Shop');
    		$re = $db->where("username='{$username}'")->find();
    		if(empty($re)){
    			$this->display('Login/forget');
    			echo showmsg('此手机未注册，不需找回密码');exit;
    		}
    		if($password!=$ckpassword){
    			$this->display('Login/forget');
    			echo showmsg('两次密码输入不正确');exit;
    		}
    		$a = \Org\Oa\msg::check_phone_code($username,$code);
    		if($a){
    			$create['password'] = $db->autopassword($password);
    			$create['id'] = $re['id'];
    			$re = $db->save($create);
    			M('phone_code')->where(array('phone'=>$create['username']))->setField('is_use',1);
    			$this->display('Login/forget');
    			echo alert('找回密码成功,现在去登录吧',C('URL')."/Login/login");exit;
    		}else{
    			$this->display('Login/forget');
    			echo showmsg('手机验证码输入错误');exit;
    		}
    	}else{
    		$this->display('Login/forget');
    		echo showmsg('验证码输入错误');exit;
    	}
    }
    public function sendmsg(){
    	if(IS_AJAX){
    		$type = (int)I('post.type');
    		if(!empty($type)){
    			$a = 2;
    		}else{
    			$a = 1;
    		}
    		$db = D('Common/Shop');
    		if($create = $db->create($_POST,$a)){
    			$a = \Org\Web\msg::send_msg($create['username']);
    			if($a){
    				echo jsonencode(200,'发送短信成功');
    			}else{
    				echo jsonencode(300,'发送短信失败');
    			}
    		}else{
    			echo jsonencode(300,$db->getError());
    		}
    	}
    }
    /**
     * 退出
     */
    public function out_login(){
    	session_destroy();
    	//unset(s['eventor_shop_user_id']);
    	session('bxd_shop_user_id',null);
    	$this->redirect('Login/login');
    }
    
}